﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace calculation.Models
{
    public class MaterialRepository
    {
         private UchetDataContext _datacontext;

        public MaterialRepository(UchetDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public IEnumerable<Material> GetMaterials()
        {
            return _datacontext.Materials.OrderBy(m => m.Name);
        }

        public Material GetMaterials(int materialId)
        {
            return _datacontext.Materials.SingleOrDefault(m => m.MaterialId == materialId);
        }
        public Material CreateMaterial(string name, string typeMaterial, int amount) 
        {
            Material m = new Material { Name = name, TypeMaterial = typeMaterial, Amount = amount, ProducerId = 1};
            _datacontext.Materials.InsertOnSubmit(m);
            _datacontext.SubmitChanges();
            return m;
        }
        public void UpdateMaterial(Material m)
        {
            Material dbMaterial = GetMaterials(m.MaterialId);
            dbMaterial.Name = m.Name;
            dbMaterial.TypeMaterial = m.TypeMaterial;
            dbMaterial.Amount = m.Amount;
            _datacontext.SubmitChanges();
        }

        public void DeleteMaterial(int materialId)
        {
            Material m = GetMaterials(materialId);
            _datacontext.Materials.DeleteOnSubmit(m);
            _datacontext.SubmitChanges();   
        }
    }
}